// ViafirmaCppExampleMain.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "viafirmaClient.h"
#include <string>
#include <iostream>

#include <fstream>



/**
 * Load file into byte array (for test)
 */
int load_file_to_memory(const char *filename, unsigned char **result) ;

int _tmain(int argc, _TCHAR* argv[])
{
		try{
			std::string urlViafirma("http://testservices.viafirma.com/viafirma");
			//std::string urlViafirma("http://pc160.viavansi.com/viafirma-business");
			viafirmaClient v(urlViafirma);

			std::string result=v.ping("test message");
			std::cout<<result;

			//***************
			// Test Sign document (CMD/PKCS7 format)
			//***************

			std::string nameFile("test.png"); //anything name.
			// Load data to sign (somewhere, for example one image file)
			// Example load file
			unsigned char * dataToSign ;	
			int dataSize=load_file_to_memory("test.png",&dataToSign);

			//unsigned char  dataToSign[10]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
			//int dataSize=sizeof(dataToSign);
	
			// alias and pass of certificate.
			std::string alias("xnoccio");
			std::string password("12345");

			// Call to server 	
			result=v.signByServerWithSha1(nameFile,dataToSign,dataSize,alias,password,TYPE_FORMAT_SIGN_BINARRY);
			std::cout << "TEST Sign method. Id sign:"<<result;
			std::cout<<"\nYou can see the verification info in:\n "<<urlViafirma<<"/v/"+result;
	


			//***************
			// get signed Document (CMD/PKCS7 format)
			//***************

			std::string idSign=result;
			SignedDocument * info=v.getSignedDocumentData(idSign);
			// Signed Document Data: CMD/PKCS7 data: info.data;
			std::cout<<"\n id:"<<info->id;
			//std::cout<<"\n Signed Document size:"<<info.data;
			std::cout<<"\n Signed Document size:"<<info->id;

			std::ofstream output;
			output.open("out.p7s",std::ios::binary);
			output.write(info->data, info->dataLen);
			output.flush();
			output.close();

			unsigned char * signedDocument=(unsigned char*)info->data;
			int signdDocumentSize=info->dataLen;
			// Examble validation data with SHA1
			bool validationOK=v.validateWithSha1(dataToSign,dataSize,signedDocument,signdDocumentSize,TYPE_FORMAT_SIGN_BINARRY);
			std::cout<<"\n Validation:"<<validationOK;

			//	Full validation
			validationOK=v.checkOrignalVSSignedDocument(dataToSign,dataSize,signedDocument,signdDocumentSize,TYPE_FORMAT_SIGN_BINARRY);
			std::cout<<"\n Full Validation:"<<validationOK;

			delete info;
			delete dataToSign;
		}catch(char * msg){
			std::cout<<"\nException:"<<msg;
		}

	//wait
	std::string msg;
	std::cin>>msg;
	return 0;
}

/**
 * Load file into byte array (for test)
 */
int load_file_to_memory(const char *filename, unsigned char **result) 
{ 
	int size = 0;
	FILE *f = fopen(filename, "rb");
	if (f == NULL) 
	{ 
		*result = NULL;
		return -1; // -1 means file opening fail 
	} 
	fseek(f, 0, SEEK_END);
	size = ftell(f);
	fseek(f, 0, SEEK_SET);
	*result = (unsigned char *)malloc(size+1);
	if (size != fread(*result, sizeof(char), size, f)) 
	{ 
		free(*result);
		return -2; // -2 means file reading fail 
	} 
	fclose(f);
	(*result)[size] = 0;
	return size;
}
